"use strict";

const path = require("path");
const expect = require("chai").expect;
const rm = require("rimraf-promise");
const FileService = require(path.join(__dirname,
                                            "..",
                                            "..",
                                            "src",
                                            "services",
                                      "FileService.js"));
const sandboxPath = path.join(__dirname, "..", "fixtures", "tmp");

/** @test {FileService} */
describe("FileService", function() {
  let subject = new FileService();

  /** @test {FileService#constructor} */
  it("#constructor", function() {
    expect(subject).to.be.instanceof(FileService);
  });

  /** @test {FileService#concatFile} */
  describe("#concat", function() {
    it("when no path", function() {
      expect(subject.concat("app", sandboxPath)).to.equal(sandboxPath + "/app");
    });
    it("when path and name", function() {
      expect(subject.concat("app", path.join(sandboxPath, "project"))).to.equal(sandboxPath + "/project/app");
    });
  });

  /** @test {FileService#create} */
  it("#create", function(done) {
    subject.create(path.join(sandboxPath, "app"))
      .then(() => rm(path.join(sandboxPath, "app")))
      .then(() => {
        done();
      });
  });

  /** @test {FileService#findOrCreate} */
  describe("#findOrCreate", function(done) {
    it("find a single file", function(done) {
      subject.create(path.join(sandboxPath, "app"))
        .then(() => subject.findOrCreate({name: "app", parent: sandboxPath}))
        .then(function(result) {
          expect(result).to.equal(true);
          done();
        });
    });
    it("create a single a file", function(done) {
      rm(path.join(sandboxPath, "app"))
        .then(() => subject.findOrCreate({name: "app", parent: sandboxPath}))
        .then(() => subject.findOrCreate({name: "app", parent: sandboxPath}))
        .then((result) => {
          expect(result).to.equal(true);
        })
        .then(() => rm(path.join(sandboxPath, "app")))
        .then(() => done());
    });
  });
});


